package cn.gailvlun.gll.presentation.emotion.filter;

import android.app.Dialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.github.markzhai.recyclerview.BaseViewAdapter;
import com.github.markzhai.recyclerview.SingleTypeAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.DialogEmotionFilterBinding;
import cn.gailvlun.gll.net.emotion.FilterEnum;
import cn.gailvlun.gll.net.emotion.RefreshReq;
import cn.gailvlun.gll.presentation.base.App;
import cn.gailvlun.gll.presentation.emotion.EmotionSquareActivity;
import cn.gailvlun.gll.presentation.emotion.EmotionSquareViewModel;
import cn.gailvlun.gll.presentation.emotion.widget.DividerDecoration;


public class EmotionFilterDialog extends Dialog {

    public DialogEmotionFilterBinding mBinding;
    private EmotionFilterAdapter mAdapter;
    private List<Filter> mDatas = new ArrayList<Filter>() {
        {
            add(new Filter(FilterEnum.ALL));
            add(new Filter(FilterEnum.N));
            add(new Filter(FilterEnum.P));

            add(new Filter(FilterEnum.CANCEL, ColorStateList.valueOf(App.getInstance().getResources().getColor(R.color.apricot))));
        }
    };
    private EmotionSquareActivity mActivity;
    private EmotionSquareViewModel mViewModel;

    public EmotionFilterDialog(@NonNull Context context) {
        super(context, cn.gailvlun.gll.net.R.style.BaseDialog_Bottom);
        mActivity = (EmotionSquareActivity) context;
        mViewModel = (EmotionSquareViewModel) ((EmotionSquareActivity) context).getViewModel();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initWindow();


        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dialog_emotion_filter, null, false);
        mAdapter = new EmotionFilterAdapter(mActivity);
        mAdapter.setPresenter(new EmotionFilterPresenter());
        mAdapter.addAll(mDatas);

        mBinding.rv.setAdapter(mAdapter);
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mActivity));
        mBinding.rv.addItemDecoration(new DividerDecoration(mActivity));
        setContentView(mBinding.getRoot());
    }

    private void initWindow() {
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);

        window.setWindowAnimations(cn.gailvlun.gll.net.R.style.BaseDialog_Bottom_UP);
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;

        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }

    public EmotionFilterAdapter getAdapter() {
        return mAdapter;
    }

    class EmotionFilterAdapter extends SingleTypeAdapter<Filter> {

        EmotionFilterAdapter(Context context) {
            super(context);
        }

        @Override
        protected int getLayoutRes() {
            return R.layout.item_dialog_emotion_filter;
        }

        public List<Filter> getData() {
            return mCollection;

        }
    }


    public class EmotionFilterPresenter implements BaseViewAdapter.Presenter {
        public void onFilter(Filter filter) {

            RefreshReq req = mActivity.req;
            req.setId(0);
            switch (filter.filterEnum) {
                case ALL:
                case N:
                case P:

                    if (filter.filterEnum == FilterEnum.ALL) {
                        req.setFilterEnum(null);
                    } else {
                        req.setFilterEnum(filter.filterEnum);

                    }
                    mViewModel.getFilterTextObservable().set(filter.filterEnum.des);
                    mActivity.getData(req);
                    break;
                case CANCEL:
                    break;
            }
            dismiss();
        }

    }

    public static class Util {
        public static String getText(Filter filter) {
            switch (filter.filterEnum) {
                case ALL:
                    return "全部";
                case N:
                    return "只看负面情绪";
                case P:
                    return "只看正面情绪";


                case CANCEL:
                    return "取消";
                default:
                    return "";
            }

        }
    }


}
